package com.jianxiu.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jianxiu.bean.Page;
import com.jianxiu.bean.Result;
import com.jianxiu.bean.SearchParam;
import com.jianxiu.bean.SysConfig;
import com.jianxiu.service.SysConfigService;

/**
 * Created by qiao on 2017/5/11.
 */
@Controller
@RequestMapping("sysConfig")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    @RequestMapping("list")
    public String list() {
        return "sysConfig/list";
    }

    @RequestMapping("list-data")
    @ResponseBody
    public Page<SysConfig> listData(SearchParam sp) {
        return sysConfigService.page(sp.getSp());
    }

    @RequestMapping("form")
    public String form(@RequestParam(value = "id", required = false) Long id, Model model) {
        if (id != null) {
            model.addAttribute("info", sysConfigService.getById(id));
        } else {
            model.addAttribute("info", new SysConfig());
        }
        return "sysConfig/form";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(SysConfig sysConfig) {
        if (null == sysConfig || sysConfig.getId() == null) {
            return Result.byMsg("参数错误");
        }
        SysConfig config = sysConfigService.getById(sysConfig.getId());
        if (config == null) {
            return Result.byMsg("参数错误");
        }
        int result = sysConfigService.update(sysConfig);
        return result > 0 ? Result.success() : Result.byMsg("失败");
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam(value = "id", required = true) Long id) {
        int result = sysConfigService.delete(id);
        return result > 0 ? Result.success() : Result.byMsg("失败");
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(SysConfig sysConfig) {
        int result = sysConfigService.add(sysConfig);
        return result > 0 ? Result.success() : Result.byMsg("失败");
    }
}
